package cn.cloudtogo.server.redis;

import cn.cloudtogo.common.types.Result;
import cn.cloudtogo.support.redis.RedisConnectionHelper;
import cn.cloudtogo.support.redis.RedisExternalDatasourceCreateParam;
import cn.cloudtogo.support.redis.RedisExternalDatasourceService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;

import static cn.cloudtogo.server.WebResponse.accepted;
import static cn.cloudtogo.server.WebResponse.ok;

/**
 * @author yaolianhua789@gmail.com
 **/
//@RestController
//@RequestMapping("/datasources/redis")
public class RedisExternalDatasourceController {

    private final RedisExternalDatasourceService datasourceService;

    public RedisExternalDatasourceController(RedisExternalDatasourceService datasourceService) {
        this.datasourceService = datasourceService;
    }

    @GetMapping("/healthy")
    public ResponseEntity<Result<Boolean>> isValidConnection(@RequestParam String url) {
        RedisConnectionHelper.ConnectionValidBind bind = RedisConnectionHelper.isValidConnection(url);
        return ok(bind.isValid());
    }

    @PostMapping
    public ResponseEntity<Result<String>> post(@Valid @RequestBody RedisExternalDatasourceCreateParam param) {
        return accepted(datasourceService.create(param));
    }

}
